package net.mfjassociates.financial;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPathExpressionException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class TestOfx {

	private static final String LOGIN_URL = "https://www1.royalbank.com/cgi-bin/rbaccess/rbcgi3m01";

	/**
	 * @param args
	 * @throws IOException
	 * @throws XPathExpressionException
	 */
	public static void main(String[] args) throws XPathExpressionException,
			IOException {
		// TODO Auto-generated method stub
		String token = TestNet.retrieveToken();

		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter("http.useragent", "Test Client");
		httpclient
				.getParams()
				.setParameter("Accept",
						"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

		BufferedReader br = null;

		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("FromPreSignIn_SIP", "Y"));
		qparams.add(new BasicNameValuePair("LANGUAGE", "ENGLISH"));
		qparams.add(new BasicNameValuePair("F30", "1,X001,5,K1,2,Q1"));
		qparams.add(new BasicNameValuePair("SST", token));
		qparams.add(new BasicNameValuePair("F6", "1"));
		qparams.add(new BasicNameValuePair("F7", "S0"));
		qparams.add(new BasicNameValuePair("F21", "PB"));
		qparams.add(new BasicNameValuePair("F22", "HT"));
		qparams.add(new BasicNameValuePair("CHKCLICK", "N"));
		qparams.add(new BasicNameValuePair("NNAME", ""));
		qparams.add(new BasicNameValuePair(
				"RSA_DEVPRINT",
				"version%253D1%2526pm%255Ffpua%253Dmozilla%252F5%252E0%2520%2528windows%253B%2520u%253B%2520windows%2520nt%25206%252E1%253B%2520en%252Dus%253B%2520rv%253A1%252E9%252E2%252E16%2529%2520gecko%252F20110319%2520firefox%252F3%252E6%252E16%257C5%252E0%2520%2528Windows%253B%2520en%252DUS%2529%257CWin32%2526pm%255Ffpsc%253D24%257C1280%257C1024%257C1024%2526pm%255Ffpsw%253D%257Cqt1%257Cqt2%257Cqt3%257Cqt4%257Cqt5%257Cqt6%257Cpdf%257Cdef%257Cswf%2526pm%255Ffptz%253D%252D4%2526pm%255Ffpln%253Dlang%253Den%252DUS%257Csyslang%253D%257Cuserlang%253D%2526pm%255Ffpjv%253D0%2526pm%255Ffpco%253D1"));
		qparams.add(new BasicNameValuePair("ROLLOUT", "PORTAL"));
		qparams.add(new BasicNameValuePair("K1", "4519035007123856"));
		qparams.add(new BasicNameValuePair("N1", ""));
		qparams.add(new BasicNameValuePair("Q1", ""));
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(qparams, "UTF-8");
		HttpPost httppost = new HttpPost(LOGIN_URL);
		httppost.setEntity(entity);

		try {
			HttpResponse response = httpclient.execute(httppost);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String responseBody = responseHandler.handleResponse(response);
		} finally {
			httpclient.getConnectionManager().shutdown();
		}

	}

}
